package com.dstz.demo.rest.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.aop.annotion.CatchErr;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.core.util.RestTemplateUtil;
import com.dstz.base.rest.BaseController;
import com.dstz.bus.api.remote.RemoteBusinessData;
import com.dstz.demo.core.manager.DemoManager;
import com.dstz.demo.core.model.Demo;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 案例 控制器类<br/>
 * 
 * @author aschs
 *    
 */
@RestController
@RequestMapping("/demo/demo/")
@SuppressWarnings("unchecked")
@Api(description = "DEMO 案例模块")
public class DemoController extends BaseController<Demo> {
	@Resource
	DemoManager demoManager;
	private static Logger log = LoggerFactory.getLogger(DemoController.class);

	@Override
	protected String getModelDesc() {
		return "案例";
	}
	
	 
	 
	@RequestMapping("testRemote")
	@CatchErr(write2response = true, value = "testRemote失败")
	public void testRemote(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = "http://localhost:8080/agile-bpm-platform/demo/demo/saveData";
		RemoteBusinessData<JSONObject> remoteBusinessData = new RemoteBusinessData<>();
		ResultMsg<JSONObject> resp = RestTemplateUtil.post(url, remoteBusinessData, ResultMsg.class);
		System.out.println(resp);
	}

	@RequestMapping("log")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="form",dataType="String",name="logger",value="日志"),
		@ApiImplicitParam(paramType="form",dataType="String",name="level",value="级别")})
	public Object log(HttpServletRequest request, HttpServletResponse response) {
//		response.addHeader("Access-Control-Allow-Origin", "*");
		String loggerName = request.getParameter("logger");
		List<ch.qos.logback.classic.Logger> loggers = null;
		if(!StringUtils.isBlank(loggerName)) {
			ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(loggerName);
			loggers = Arrays.asList(logger);
			if(!loggers.isEmpty()) {
				String levelName = request.getParameter("level");
				Level level = Level.toLevel(levelName, null);
				log.info("change logger:{} level from:{} to:{}", logger.getName(), logger.getLevel(), level);
				logger.setLevel(level);
			}
		}
		if(loggers == null) {
			LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
			loggers = lc.getLoggerList();
		}
		log.info("check logger level, loggers:{}", loggers.size());
		List<Map<String, String>> list = new ArrayList<>();
		for(ch.qos.logback.classic.Logger logger : loggers) {
			HashMap<String, String> map = new HashMap<>();
			map.put("logger", logger.getName());
			map.put("level", Objects.toString(logger.getLevel(), ""));
			list.add(map);
		}
		return Collections.singletonMap("loggers", list);
	}	

}
